package com.gzhryc.shared_device.oem.code.config.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayConfig;
import com.gzhryc.shared_device.common.payment.services.WXPayConfigService;
import com.gzhryc.shared_device.oem.code.config.dao.AppResourcePreloadDao;
import com.gzhryc.shared_device.oem.code.config.dao.db.AppResourcePreload;
import com.gzhryc.shared_device.oem.code.config.services.dto.AppResourcePreloadSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AppResourcePreloadService {

    static Logger log = Logger.getLogger(AppResourcePreloadService.class);

    public static AppResourcePreloadService self() {
        return MultiDBTools.getService(AppResourcePreloadService.class, true);
    }

    public static AppResourcePreloadService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AppResourcePreloadService.class, true);
    }

    AppResourcePreloadDao dao;

    public AppResourcePreloadService(String jdbcKey) {
        this.dao = new AppResourcePreloadDao(jdbcKey);
    }

    public List<AppResourcePreload> findBySearch(AppResourcePreloadSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(AppResourcePreloadSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public AppResourcePreload getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(AppResourcePreload appPreload) throws LogicException {
        appPreload.setCreateDate(new Date());
        WXPayConfig config = WXPayConfigService.self().getByAppId(appPreload.getAppId());
        if(config == null){
            throw new LogicException("应用ID不存在");
        }
        try {
            return dao.insert(appPreload) >= 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(AppResourcePreload appPreload) throws LogicException {
        if(StringTools.isNotBlank(appPreload.getAppId())) {
            WXPayConfig config = WXPayConfigService.self().getByAppId(appPreload.getAppId());
            if (config == null) {
                throw new LogicException("应用ID不存在");
            }
        }
        try {
            return dao.updateNotNull(appPreload) >= 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id){
        try {
            return dao.deleteById(id)>0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
